/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agentes;

import Ontologia.OA;
import hibernate.NewHibernateUtil;
import Ontologia.OAOntology;
//
import jade.content.*;
import jade.content.lang.*;
import jade.content.lang.Codec.CodecException;
import jade.content.lang.sl.*;
import jade.content.onto.*;
import jade.core.*;
import jade.core.behaviours.*;
import jade.domain.DFService;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
import jade.domain.FIPAException;
import jade.lang.acl.*;
import jade.wrapper.AgentController;
import jade.wrapper.ControllerException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import vista.nuevoOA;

public class GeneradorContenidos extends Agent {

    dao.Oa oa = new dao.Oa();
    private Codec codec = new SLCodec();
    //Verificar nombre de la ontología
    private Ontology ontologia = OAOntology.getInstance();
    private static SessionFactory sessionFactory = null;
    private AgentController ag;

    protected void setup() {

        getContentManager().registerLanguage(codec);
        getContentManager().registerOntology(ontologia);


        GeneracionContenido gc = new GeneracionContenido(this);
        addBehaviour(gc);

        jade.wrapper.AgentContainer containerController = this.getContainerController();
        // Enable O2A communication
        setEnabledO2ACommunication(true, 0);

        try {
            ag = containerController.getAgent("Generador");
//            nuevoOA newOA = new nuevoOA(ag);
//            newOA.setVisible(true);
        } catch (ControllerException ex) {
            Logger.getLogger(OA.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    class GeneracionContenido extends CyclicBehaviour {

        public GeneracionContenido(Agent a) {
            super(a);
        }

        public void action() {
              System.out.println("entramos al action().");
            MessageTemplate mt = MessageTemplate.and(
                    MessageTemplate.MatchLanguage(codec.getName()),
                    MessageTemplate.MatchOntology(ontologia.getName()));
            ACLMessage mensaje = blockingReceive(mt);
            if (mensaje == null) {
                
                   Session session = null;
                    oa = (dao.Oa) myAgent.getO2AObject();

                    try {
                        try {
                            sessionFactory = NewHibernateUtil.getSessionFactory();
                            session = sessionFactory.openSession();
                            Transaction tx = session.beginTransaction();

                            session.beginTransaction();
                            session.save(oa);
                            session.getTransaction().commit();

                            System.out.println(oa.getTitulo());

                            tx.commit();
                            System.out.println("Mensaje de material generado enviado.");
                        } catch (Exception e) {
                            System.out.println(e.getMessage());
                        }
                    } finally {
                        session.close();
                    }
              

            }


        }
    }

}
